
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "uac.h"

static volatile int keepRunning = 1;

void *sip_cmd_loop(void *param)
{
    char command;
    int flag1;

    int call_id, dialog_id;

    while (keepRunning)
    {
        //输入命令
        printf("Please input the command:\n");
        scanf("%c", &command);
        getchar();

        switch (command)
        {
        case 'r':
            printf("Register sip server!\n");
            sip_register("192.168.1.110", "192.168.1.110", 5060, "1004", "1234");
            break;

        case 'u':
            printf("UnRegister sip server!\n");
            sip_unregister();
            break;

        case 'i': //INVITE，发起呼叫请求
            printf("Invite!\n");
            call_id = sip_invite("133");
            break;

        case 'a':
            printf("Answer!\n");
            sip_answer(call_id);
            break;

        case 'h': //挂断
            printf("Hangup!\n");
            sip_hangup(call_id);
            break;

        case 's': //传输INFO方法
            /*eXosip_call_build_info(ctx,dialog_id,&info);
            snprintf(tmp,4096,"\nThis is a sip message(Method:INFO)");
            osip_message_set_body(info,tmp,strlen(tmp));
            //格式可以任意设定，text/plain代表文本信息;
            osip_message_set_content_type(info,"text/plain");
            eXosip_call_send_request(ctx,dialog_id,info);*/
            break;

        case 'm':
            //传输MESSAGE方法，也就是即时消息，和INFO方法相比，我认为主要区别是：
            //MESSAGE不用建立连接，直接传输信息，而INFO消息必须在建立INVITE的基础上传输
            /*printf("the method : MESSAGE\n");
            eXosip_message_build_request(ctx,&message,"MESSAGE",dest_call,source_call,NULL);
            //内容，方法，      to       ，from      ，route
            snprintf(tmp,4096,"This is a sip message(Method:MESSAGE)");
            osip_message_set_body(message,tmp,strlen(tmp));
            //假设格式是xml
            osip_message_set_content_type(message,"text/xml");
            eXosip_message_send_request(ctx,message);*/
            break;

        case 'q':
            sip_exit();
            keepRunning = 0;
            break;
        }
    }

    return NULL;
}

int main(int argc, char *argv[])
{
    //osip_message_t *reg = NULL;
    //osip_message_t *info = NULL;
    //osip_message_t *message = NULL;

    int call_id, dialog_id;
    int i, flag;
    int flag1 = 1;
    
    //identify和register这一组地址是和source和destination地址相同的
    //在这个例子中，uac和uas通信，则source就是自己的地址，而目的地址就是uac1的地址

    //pthread_t tha;
    //int reta;

    printf("r   注册\n");
    printf("u   注销\n");
    printf("i   发起呼叫请求\n");
    printf("a   接听\n");
    printf("h   挂断\n");
    printf("q   推出程序\n");
    printf("s   执行方法INFO\n");
    printf("m   执行方法MESSAGE\n");

    //初始化
    sip_init();

    
    //pthread_join(tha, &reta);
    keepRunning = 1;
    sip_cmd_loop(NULL);

    return (0);
}
